#include "tabletemp.h"
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

string TableTemp::tableTempName;
string TableTemp::homePath;

const string &TableTemp::getTableTempName()
{
	if (tableTempName == ""){
		string hostName;
		char pid[32];
		getHostName(hostName);
		sprintf(pid, "_%d", getpid());

		tableTempName  = "tmp_";
		tableTempName += hostName;
		tableTempName += pid;
	}
	return tableTempName;
}

const string &TableTemp::getHomePath()
{
	if (homePath == ""){
		homePath = ::getenv("HOME");
	}
	return homePath;
}

bool TableTemp::getHostName(string &hostName)
{
	char tmp[256];
	
	if (::gethostname(tmp, 256) == -1){
		printf("gethostname error,errno=%d\n", errno);
		return false;
	}
	hostName = tmp;
	return true;
}

int TableTemp::getpid()
{
	return ::getpid();
}

